﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NutriLeaf.Web.Core.Food
{
    public class FoodIndigrident:IFoodIngredient
    {
        public void AddFoodIngredient(Model.FoodIngredient foodIngredient, Model.HealthRating healthRating, Model.NutritionInfo nutritionInfo, ref Model.NutriLeafDBEntities context)
        {
            Model.FoodIngredient food = new Model.FoodIngredient();
            food.HealthRating = healthRating;
            food.NutritionInfo = nutritionInfo;
          
            food.DateTimeStamp = DateTime.Now;
            context.FoodIngredients.AddObject(food);
            context.SaveChanges();
        }

        public void AddFoodIngredient(string ingredientName, string quantity, string descirption, Model.HealthRating healthRating, Model.NutritionInfo nutritionalInfo, ref Model.NutriLeafDBEntities context)
        {
            Model.FoodIngredient food = new Model.FoodIngredient();
            food.IngredientName = ingredientName;
            food.IngredientDescription = descirption;
            food.HealthRating = healthRating;
            food.NutritionInfo = nutritionalInfo;
            context.FoodIngredients.AddObject(food);
            context.SaveChanges();
        }

        public void RemoveFoodIngredient(int foodIngredientId, ref Model.NutriLeafDBEntities context)
        {
            if (context.FoodIngredients.Where(p => p.FoodIngredientID == foodIngredientId).Count() != 0) {
                context.DeleteObject(context.FoodIngredients.Where(p => p.FoodIngredientID == foodIngredientId).First());
                context.SaveChanges();
            }
        }

        public List<Model.FoodIngredient> GetFoodIngredientInFood(int foodId, ref Model.NutriLeafDBEntities context)
        {
            return context.FoodIngredients.Where(p => p.FoodIngredientID == foodId).ToList();
        }

        public void UpdateFoodIngredient(Model.FoodIngredient foodIngredient, Model.HealthRating healthRating, Model.NutritionInfo nutritionInfo, ref Model.NutriLeafDBEntities context)
        {
            //not implemented
        }

        public List<Model.Food> GetFoodsWithIngredient(int foodIngredientId, ref Model.NutriLeafDBEntities context)
        {
            List<Model.Food> list = new List<Model.Food>();
            foreach (Model.Food food in context.Foods.ToArray()) {
                if (food.FoodIngredientsInFoods.Where(p => p.FoodIngredientID == foodIngredientId).Count() != 0) {
                    list.Add(food);
                }
            }
            return list;
        }

        public void AddFoodIngredientToFood(int foodIngredientId, int foodId, ref Model.NutriLeafDBEntities context)
        {
            Model.Food food = context.Foods.Where(p => p.FoodID == foodId).First();
            Model.FoodIngredientsInFood f = new Model.FoodIngredientsInFood();
            f.FoodID = foodId;
            f.FoodIngredientID = foodIngredientId;

            food.FoodIngredientsInFoods.Add(f);
            context.SaveChanges();

        }

        public void RemoveFoodIngredientFromFood(int foodIngredientId, int foodId, ref Model.NutriLeafDBEntities context)
        {
           List<Model.FoodIngredientsInFood> fods= context.FoodIngredientsInFoods.Where(p => p.FoodIngredientID == foodIngredientId).ToList();
           foreach (Model.FoodIngredientsInFood indi in fods) {
               context.FoodIngredientsInFoods.DeleteObject(indi);
             
           }
           context.SaveChanges();
        }
    }
}